package ua.maxicash.dao;

import org.hibernate.CacheMode;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import ua.maxicash.vo.*;

import java.sql.Time;
import java.util.List;

@Repository("workDao")
public class WorkDaoImpl implements WorkDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    HibernateTemplate hibernateTemplate;

    @Override
    @Transactional
    public void addWork(Work work) {
        hibernateTemplate.saveOrUpdate(work);
    }


    @Override
    @Transactional
    public void updateWork(Work work) {
        //TODO resolve this f//cken problem
        for (WorkingDay wd : work.getSchedule()) {
            hibernateTemplate.saveOrUpdate(wd);
        }
        hibernateTemplate.flush();
        hibernateTemplate.merge(work);
        //hibernateTemplate.saveOrUpdate(work);
    }


    @Override
    public List<Work> listWork() {

        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Work.class);
        List<Work> list = criteria.list();
        session.close();

        return list;


    }

    @Override
    public List<JobType> listJobType() {

        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(JobType.class);
        criteria.setCacheable(true);
        criteria.setCacheMode(CacheMode.NORMAL);
        List<JobType> list = criteria.list();
        session.close();

        return list;
    }

    @Override
    @Transactional
    public void addWorkersFreeTime(WorkersFreeTime wft) {

        hibernateTemplate.saveOrUpdate(wft);

    }

    @Override
    public List<Worker> listWorkersWithStartTime(Time startTime) {


        DetachedCriteria dc = DetachedCriteria.forClass(Worker.class);
        dc.createAlias("scheduleList", "freeTime", CriteriaSpecification.LEFT_JOIN);
        dc.add(Restrictions.eq("freeTime.startTime", startTime));
        List<Worker> workers = hibernateTemplate.findByCriteria(dc);
        return workers;
    }

    @Override
    public Work getWorkById(Long id) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Work.class);
        criteria.add(Restrictions.eq("idWork", id));
        Work work = (Work) criteria.uniqueResult();
        session.close();

        return work;
    }

    @Override
    public JobType selectJobType(Long id) {
//
//		DetachedCriteria crit = DetachedCriteria.forClass(JobType.class);
//		Criterion criterion =
//
//		hibernateTemplate.findByCriteria(crit);
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(JobType.class);
        //TODO improve productivity
        criteria.add(Restrictions.eq("idJob", id));
        JobType jt = (JobType) criteria.uniqueResult();
        session.clear();
        session.close();

        return jt;
    }
}
